package com.gp.library.utils;

import android.text.TextUtils;
import android.util.Patterns;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jackdeng on 2021/4/13.
 */


public class StringUtils {


    static public String punctuation="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：\"\"“”’。，、？《》]";
    static public String PATH_ILLEGAL_CHARS = "()（）";

    /**
     * 是否是中文
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }

        return false;
    }

    /**
     * 是否是英文
     * @param charaString
     * @return
     */
    public static boolean isEnglish(String charaString){
        return charaString.matches("^[a-zA-Z]*");
    }

    public static boolean isChinese(String str){

        String regEx = "[\\u4e00-\\u9fa5]+";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);

        if(m.find())
            return true;
        else
            return false;
    }

    public static boolean isDigitsOnly(String str){
        if (TextUtils.isEmpty(str))
            return false;
        return TextUtils.isDigitsOnly(str);
    }

    /**
     * 去掉字符串中的标点符号
     * @param input
     * @return
     */
    public static String removePunctuation(String input) {
        if (TextUtils.isEmpty(input))
            return null;

        StringBuffer buffer = new StringBuffer();
        int start = 0;
        while (start < input.length()) {
            String s = String.valueOf(input.charAt(start));
            if (!isSymbol(s)) {
                buffer.append(s);
            }
            ++start;
        }
        return buffer.toString();
    }

    public static boolean isNullOrEmpty(final String string) {
        return (string == null || string.trim().length() <= 0);
    }

    public static boolean isNotBlank(final String string) {
        return (string != null && string.trim().length() > 0);
    }

    public static boolean isBlank(final String string) {
        return (string != null || string.trim().length() <= 0);
    }

    public static boolean isUrl(String url) {
        return !isNullOrEmpty(url) && Patterns.WEB_URL.matcher(url).matches();
    }

    public static String trimPunctuation(String input) {
        input = trim(input);
        if (StringUtils.isNullOrEmpty(input)) {
            return input;
        }

        int start = 0;
        while (start < input.length() - 1) {
            if (!punctuation.contains(String.valueOf(input.charAt(start)))) {
                break;
            }
            ++start;
        }

        int end = input.length() - 1;
        while (end >= 0) {
            if (!punctuation.contains(String.valueOf(input.charAt(end)))) {
                break;
            }
            --end;
        }
        input = input.substring(start, end + 1);
        return input;
    }

    public static String trim(String input) {
        if (StringUtils.isNotBlank(input)) {
            input = input.trim();
            input = input.replace("\u0000", "");
            input = input.replace("\\u0000", "");
            input = input.replaceAll("\\u0000", ""); // removes NUL chars
            input = input.replaceAll("\\\\u0000", ""); // removes backslash+u0000
        }
        return input;
    }

    public static boolean isSymbol(CharSequence c) {
        return punctuation.contains(c);
    }

    public static boolean checkFontPathValid(String path) {
        if (StringUtils.isNullOrEmpty(path)) {
            return false;
        }
        for (int i = 0; i < path.length(); i++) {
            if (PATH_ILLEGAL_CHARS.contains(String.valueOf(path.charAt(i)))) {
                return false;
            }
        }
        return true;
    }

    public static String getProgress(int pageIndex, int totalPage) {
        if (totalPage > 0) {
            return String.format(" %.1f%%", pageIndex * 100 / (float)totalPage);
        }
        return "";
    }
}
